package com.wangkang.eduservice.controller.front;
import com.wangkang.commonutils.R;
import com.wangkang.commonutils.model.vo.CourseOrderVo;
import com.wangkang.eduservice.client.OrdersClient;
import com.wangkang.eduservice.entity.EduCourse;
import com.wangkang.eduservice.entity.query.EduCourseQuery;
import com.wangkang.eduservice.entity.vo.EduChapterVo;
import com.wangkang.eduservice.entity.vo.EduCourseWebVo;
import com.wangkang.eduservice.service.EduChapterService;
import com.wangkang.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;


@Api(tags = "课程C端服务")
@RestController
@RequestMapping("/eduservice/courseFront")
//@CrossOrigin
public class CourseFrontController {

    @Autowired
    private  EduCourseService courseService;
    @Autowired
    private  EduChapterService chapterService;
    @Autowired
    private  OrdersClient ordersClient;

    public CourseFrontController(EduCourseService courseService, EduChapterService chapterService, OrdersClient ordersClient) {
        this.courseService = courseService;
        this.chapterService = chapterService;
        this.ordersClient = ordersClient;
    }

    @ApiOperation("条件分页查询课程")
    @PostMapping("/getFrontCourse/{page}/{size}")
    public R getFrontCourseList(@PathVariable long page,@PathVariable long size,
         EduCourseQuery courseQuery){

        Page<EduCourse> coursePage = new Page<>(page,size);
        Map<String,Object> map = courseService.getCourseFrontList(coursePage,courseQuery);
        //调用的data方法是data(Map<String,Object> map)。
        return R.ok().data(map);
    }

//    @ApiOperation("根据课程id，查询课程信息")
//    @GetMapping("/getFrontCourseInfo/{courseId}")
//    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
//        //根据课程id，查询课程的基本信息
//        EduCourseWebVo courseWebVo =  courseService.getBaseCourseInfo(courseId);
//
//        //根据课程id 和会员id 查询当前课程是否已经支付过了
//        String memberId = JwtUtils.getMemberIdByJwtToken(request);
//
//        Boolean buyCourse =false;
//        if(StringUtils.isNotBlank(memberId)){
//            buyCourse = ordersClient.isBuyCourse(courseId, memberId);
//        }
//        //根据课程id查询章节和小节
//        List<EduChapterVo> chapterVoList = chapterService.getChapterVideoByCourseId(courseId);
//        return R.ok().data("courseWebVo",courseWebVo).data("chapterVoList",chapterVoList).data("isBuy",buyCourse);
//    }


//    @GetMapping("/getCourseInfoOrder/{id}")
//    @ApiOperation(("根据课程id查询课程基本信息"))
//    public CourseOrderVo getCourseInfoOrder(@PathVariable String id){
//        EduCourseWebVo courseWebVo = courseService.getBaseCourseInfo(id);
//        CourseOrderVo courseOrderVo = new CourseOrderVo();
//        BeanUtils.copyProperties(courseWebVo,courseOrderVo);
//        return courseOrderVo;
//    }

    @GetMapping("/getCourseInfoOrder/{id}")
    @ApiOperation(("根据课程id查询课程基本信息"))
    public R getCourseInfoOrder(@PathVariable String id){
        EduCourseWebVo courseWebVo = courseService.getBaseCourseInfo(id);
        CourseOrderVo courseOrderVo = new CourseOrderVo();
        BeanUtils.copyProperties(courseWebVo,courseOrderVo);

        //根据课程id查询章节和小节
        List<EduChapterVo> chapterVideoList= chapterService.getChapterVideoByCourseId(id);
        return R.ok().data("courseOrderVo",courseOrderVo).data("chapterVideoList",chapterVideoList);

    }

}